/****************************************************************************
 * Copyright (c) 2016 - 2023 liangxiegame UNDER MIT License
 *
 * https://qframework.cn
 * https://github.com/liangxiegame/QFramework
 * https://gitee.com/liangxiegame/QFramework
 ****************************************************************************/
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace QFramework
{

    [CustomEditor(typeof(StateMachineConfig))]

    public class StateMachineConfigEditor : Editor
    {
        private StateMachineConfig config;
        private Vector2 scrollPosition;
        private bool[] foldouts;
        
        private void OnEnable()
        {
            config = (StateMachineConfig)target;
            foldouts = new bool[config.States.Count];
        }
        
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("状态机配置", EditorStyles.boldLabel);
            EditorGUILayout.Space();
            
            // 初始状态
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("初始状态:", GUILayout.Width(80));
            config.InitialStateId = EditorGUILayout.TextField(config.InitialStateId);
            EditorGUILayout.EndHorizontal();
            
            EditorGUILayout.Space();
            
            // 状态列表
            EditorGUILayout.LabelField("状态配置", EditorStyles.boldLabel);
            
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            
            // 添加新状态按钮
            if (GUILayout.Button("添加新状态", GUILayout.Height(30)))
            {
                config.States.Add(new StateConfig());
                foldouts = new bool[config.States.Count];
            }
            
            EditorGUILayout.Space();
            
            // 显示所有状态
            for (int i = 0; i < config.States.Count; i++)
            {
                var state = config.States[i];
                
                EditorGUILayout.BeginVertical("box");
                
                // 状态头部
                EditorGUILayout.BeginHorizontal();
                foldouts[i] = EditorGUILayout.Foldout(foldouts[i], $"状态 {i + 1}: {state.StateId}");
                
                // 删除按钮
                if (GUILayout.Button("删除", GUILayout.Width(50)))
                {
                    config.States.RemoveAt(i);
                    foldouts = new bool[config.States.Count];
                    break;
                }
                EditorGUILayout.EndHorizontal();
                
                if (foldouts[i])
                {
                    EditorGUI.indentLevel++;
                    
                    // 状态ID
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("状态ID:", GUILayout.Width(80));
                    state.StateId = EditorGUILayout.TextField(state.StateId);
                    EditorGUILayout.EndHorizontal();
                    
                    // 是否可被打断
                    state.CanBeInterrupted = EditorGUILayout.Toggle("可被打断", state.CanBeInterrupted);
                    
                    EditorGUILayout.Space();
                    
                    // 状态切换配置
                    EditorGUILayout.LabelField("状态切换", EditorStyles.boldLabel);
                    
                    // 添加切换条件按钮
                    if (GUILayout.Button("添加切换条件"))
                    {
                        state.Transitions.Add(new StateTransitionConfig());
                    }
                    
                    // 显示切换条件
                    for (int j = 0; j < state.Transitions.Count; j++)
                    {
                        var transition = state.Transitions[j];
                        
                        EditorGUILayout.BeginVertical("box");
                        
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField($"切换 {j + 1}", EditorStyles.boldLabel);
                        
                        if (GUILayout.Button("删除", GUILayout.Width(50)))
                        {
                            state.Transitions.RemoveAt(j);
                            break;
                        }
                        EditorGUILayout.EndHorizontal();
                        
                        // 条件名称
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField("条件名称:", GUILayout.Width(80));
                        transition.ConditionName = EditorGUILayout.TextField(transition.ConditionName);
                        EditorGUILayout.EndHorizontal();
                        
                        // 目标状态
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField("目标状态:", GUILayout.Width(80));
                        transition.TargetStateId = EditorGUILayout.TextField(transition.TargetStateId);
                        EditorGUILayout.EndHorizontal();
                        
                        // 是否强制切换
                        transition.IsForceTransition = EditorGUILayout.Toggle("强制切换", transition.IsForceTransition);
                        
                        EditorGUILayout.EndVertical();
                        EditorGUILayout.Space();
                    }
                    
                    EditorGUI.indentLevel--;
                }
                
                EditorGUILayout.EndVertical();
                EditorGUILayout.Space();
            }
            
            EditorGUILayout.EndScrollView();
            
            // 保存按钮
            if (GUILayout.Button("保存配置", GUILayout.Height(30)))
            {
                EditorUtility.SetDirty(config);
                AssetDatabase.SaveAssets();
                Debug.Log("状态机配置已保存");
            }
            
            serializedObject.ApplyModifiedProperties();
        }
    }
    
    /// <summary>
    /// 状态机配置预览窗口
    /// </summary>
    public class StateMachineConfigPreviewWindow : EditorWindow
    {
        private StateMachineConfig config;
        private Vector2 scrollPosition;
        
        [MenuItem("Window/状态机配置预览")]
        public static void ShowWindow()
        {
            GetWindow<StateMachineConfigPreviewWindow>("状态机配置预览");
        }
        
        private void OnGUI()
        {
            EditorGUILayout.LabelField("状态机配置预览", EditorStyles.boldLabel);
            EditorGUILayout.Space();
            
            // 选择配置文件
            config = (StateMachineConfig)EditorGUILayout.ObjectField("配置文件", config, typeof(StateMachineConfig), false);
            
            if (config == null) return;
            
            EditorGUILayout.Space();
            EditorGUILayout.LabelField($"初始状态: {config.InitialStateId}", EditorStyles.boldLabel);
            EditorGUILayout.Space();
            
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            
            foreach (var state in config.States)
            {
                EditorGUILayout.BeginVertical("box");
                
                EditorGUILayout.LabelField($"状态: {state.StateId}", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"可被打断: {state.CanBeInterrupted}");
                
                if (state.Transitions.Count > 0)
                {
                    EditorGUILayout.LabelField("切换条件:");
                    EditorGUI.indentLevel++;
                    
                    foreach (var transition in state.Transitions)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField($"条件: {transition.ConditionName}");
                        EditorGUILayout.LabelField($"→ {transition.TargetStateId}");
                        if (transition.IsForceTransition)
                        {
                            EditorGUILayout.LabelField("(强制)", EditorStyles.boldLabel);
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                    
                    EditorGUI.indentLevel--;
                }
                
                EditorGUILayout.EndVertical();
                EditorGUILayout.Space();
            }
            
            EditorGUILayout.EndScrollView();
        }
    }
} 
#endif